package com.lm.scau.dyb.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lm.scau.dyb.R;
import com.lm.scau.dyb.activity.base.BaseActivity;
import com.lm.scau.dyb.bean.MovieRole;
import com.lm.scau.dyb.utils.ImageLoaderUtil;
import com.lm.scau.dyb.utils.ViewHolderUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function:
 * auth：liming on 2016/8/25 10:03
 * mail：liming@wxchina.com
 */
public class CharactorAdapter extends BaseAdapter {

    private List<MovieRole> roleList;
    private BaseActivity activity;
    private Map<String, Integer> mAlphasLetters; // 首字母的位置

    public CharactorAdapter(List<MovieRole> roleList, BaseActivity activity) {
        this.roleList = roleList;
        this.activity = activity;
        mAlphasLetters = new HashMap<String, Integer>();
    }

    @Override
    public int getCount() {
        return roleList.size();
    }

    @Override
    public MovieRole getItem(int position) {
        return roleList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(null==convertView){
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_charactor_list,parent,false);
        }
        TextView alpha = ViewHolderUtils.get(convertView,R.id.alpha);
        ImageView icon = ViewHolderUtils.get(convertView,R.id.chara_icon);
        TextView name = ViewHolderUtils.get(convertView,R.id.chara_name);

        MovieRole role = getItem(position);

        name.setText(role.getRo_name());
      //  ImageLoaderUtil.getImageView(role.getRo_url(),icon);

        String currentLetter = role.getRo_alpha();
        String prevLetter = position - 1 >= 0 ? (getItem(position - 1)).getRo_alpha() : "";
        if (prevLetter.equals(currentLetter)) {
            alpha.setVisibility(View.GONE);
        } else {
            alpha.setVisibility(View.VISIBLE);
            alpha.setText(role.getRo_alpha());
        }
        return convertView;
    }
    public void updateAlphasLetters() {
        init();
    }
    public int getAlphasLetterPostion(String sLetter) {

        if (mAlphasLetters.containsKey(sLetter.toLowerCase())) {
            return mAlphasLetters.get(sLetter.toLowerCase());
        } else {
            return -1;
        }
    }
    /**
     * 重新加载首字母位置
     */
    public void init() {
        if (!mAlphasLetters.isEmpty()) {
            mAlphasLetters.clear();
        }
        for (int position = 0; position < roleList.size(); position++) {
            MovieRole role = roleList.get(position);
            String currentLetter = role.getRo_alpha();
            String prevLetter = position - 1 >= 0 ? (getItem(position - 1)).getRo_alpha() : "";
            if (prevLetter.equals(currentLetter)) {
                continue;
            } else {
                mAlphasLetters.put(role.getRo_alpha(), position);
            }
        }
    }
}
